package com.neasho.neashoadminv1.infrastructure.permission

import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component
import org.springframework.web.servlet.HandlerInterceptor
import kotlin.system.measureTimeMillis

@Component
class ApiAuthInterceptor : HandlerInterceptor {

    private val logger: Logger = LoggerFactory.getLogger(ApiAuthInterceptor::class.java)

    override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean {
        // 在请求处理前记录日志
        logger.info("Request received: ${request.method} ${request.requestURI}")
        return true
    }

    override fun afterCompletion(
        request: HttpServletRequest,
        response: HttpServletResponse,
        handler: Any,
        ex: Exception?
    ) {
        // 记录接口请求的详细信息和耗时
        val elapsedTime = measureTimeMillis {
            // 模拟业务逻辑处理
        }
        val responseBody = "Sample Response Body" // 假设这是返回的响应内容
        logger.info(
            "Request completed: ${request.method} ${request.requestURI}, " +
                    "Response: $responseBody, " +
                    "Elapsed Time: $elapsedTime ms"
        )
    }
}
